/* 
 * Copyright 2011 Carnegie Mellon University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.cmu.lti.dimple.mavuno;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Loader that converts Mavuno data into a Java object. 
 * 
 * @author Hideki Shima
 *
 */
public class MavunoDataLoader {

  public static LearnedParaphrases loadLearnedParaphrases(File f) {
    LearnedParaphrases lp = new LearnedParaphrases( f.getName().substring(0,f.getName().indexOf(".")) );
    String line = null;
    try {
      FileInputStream fis = new FileInputStream(f);
      InputStreamReader isr = new InputStreamReader(fis, "utf-8");
      BufferedReader br = new BufferedReader(isr);
      String prevId = null;
      String prevTerm = null;
      List<String> paraphrases = new ArrayList<String>();
      List<Double> confidences = new ArrayList<Double>();
      while ( (line=br.readLine())!=null ) {
        String[] values = line.split("\t+");
        if ( values.length<=1) continue;
        String id = values[1];
        if (prevId!=null && !prevId.equals(id)) {
          lp.put(prevId, new PerTermParaphraser( prevId, prevTerm, paraphrases, confidences ));
          paraphrases = new ArrayList<String>();
          confidences = new ArrayList<Double>();
        }
        paraphrases.add(values[2]);
        confidences.add(Double.parseDouble(values[3]));
        prevId = id;
        prevTerm = values[0];
      }
      lp.put(prevTerm, new PerTermParaphraser( prevId, prevTerm, paraphrases, confidences ));
      
      br.close();
      isr.close();
      fis.close();
      
    } catch (Exception e) {
      System.err.println("Problem in parsing: "+f.getName()+"\t"+line+"\t"+line.split("\t").length);
      e.printStackTrace();
    }
    return lp;
  }
  
  public static JudgedParaphrases loadAnnotatedParaphrases(File f) {
    JudgedParaphrases jp = new JudgedParaphrases( f.getName().substring(0,f.getName().indexOf(".")) );
    String line = null;
    try {
      FileInputStream fis = new FileInputStream(f);
      InputStreamReader isr = new InputStreamReader(fis, "utf-8");
      BufferedReader br = new BufferedReader(isr);
      String prevTerm = null;
      List<String> paraphrases = new ArrayList<String>();
      List<Double> mean = new ArrayList<Double>();
      List<Double> var = new ArrayList<Double>();
      List<Integer> num = new ArrayList<Integer>();
      while ( (line=br.readLine())!=null ) {
        String[] values = line.split("\t+");
        if ( values.length<=1) continue;
        String term = values[0];
        if (prevTerm!=null && !prevTerm.equals(term)) {
          jp.put(prevTerm, new PerTermJudgment( prevTerm, paraphrases, mean, var, num ));
          paraphrases = new ArrayList<String>();
          mean = new ArrayList<Double>();
          var = new ArrayList<Double>();
          num = new ArrayList<Integer>();
        }
        paraphrases.add(values[1]);
        mean.add(Double.parseDouble(values[2]));
        var.add(Double.parseDouble(values[3]));
        num.add(Integer.parseInt(values[4]));
        prevTerm = term;
      }
      jp.put(prevTerm, new PerTermJudgment( prevTerm, paraphrases, mean, var, num ));
      
      br.close();
      isr.close();
      fis.close();
      
    } catch (Exception e) {
      System.err.println("Problem in parsing: "+f.getName()+"\t"+line+"\t"+line.split("\t").length);
      e.printStackTrace();
    }
    return jp;
  }
  
}
